/***************************************************************************
 *   Copyright (C) 2009 by maarten,,,   *
 *   maarten@maarten-laptop   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "camera.h"
#include "utils/matrix.h"
#include "objects/vertex.h"

#include <QColor>

#include <math.h>
#define PI 3.14159265


namespace scene {

Camera::Camera()
 : SceneElement()
{
    m_name = "noid_cam";
    m_position = new object::Vertex(0,0,0);
    m_direction = new object::Vertex(0,0,0);
    m_up = new object::Vertex(0,0,0);
    m_fovy = 0;
    m_zDistance = 0;
    m_width=0;
    m_height=0;
}

Camera::Camera(QString name, object::Vertex *position, object::Vertex
*direction, object::Vertex *up, float fovy)
{
    m_name = name;
    m_position = position;
    m_direction = direction;
    m_up = up;
    m_fovy = fovy;

    m_width=0;
    m_height=0;
    

}

Camera::~Camera()
{
    delete m_position;
    delete m_direction;
    delete m_up;
}

/* Should be fixed for non straight viewplanes? */
void Camera::computeViewPlane(int w, int h)
{
    setViewScreen(w,h);

    if(m_fovy !=0){
        m_top = 1;
        //m_top = m_zDistance *(float)tan(PI/(float)180*(float)m_fovy/(float)2);
        float fovyRad = m_fovy*(float)PI/((float)(180));
        
        m_zDistance = 1/((float)tan(fovyRad / (float)2));
        cout << "FOVY: " << m_fovy << endl;
        cout << "Focal distance: " << m_zDistance << endl;
    }
    else{
        m_top = 0;
    }

    qDebug("T:" + QString::number(m_top).toLatin1());
}

float Camera::top(float n)
{
  return n *(float)tan(PI/(float)180*(float)m_fovy/(float)2);
}

void Camera::printElement()
{
    qDebug("Name: " + m_name.toLatin1());
    m_position->print();
    m_direction->print();
    m_up->print();
    qDebug("fovy: " + QString::number(m_fovy).toLatin1());
    qDebug("--------------------------------------");
}

}
